/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author CNTT
 * Thread lam nhiem vu tra loi truy van cua client
 */
//import java.io.*;
import java.net.*;
//import java.util.ArrayList;
//import java.util.logging.Level;
//import java.util.logging.Logger;
import java.sql.ResultSet;
import java.util.Arrays;

class ThreadReply extends Thread {

    private DatagramPacket packet;
    private DatagramSocket socket;
    //   private static Logger fLogger;

    public ThreadReply(DatagramPacket packet, DatagramSocket socket) {
        //  fLogger.info("Server");
        this.packet = packet;
        this.socket = socket;

    }

    @Override
    public void run() {
        try {
            byte[] utf8bytes = Arrays.copyOf(packet.getData(), packet.getLength());
            String st = new String(utf8bytes, "UTF8");
            byte[] data;
            // tao goi du lieu chay tu
            InetAddress ipC = packet.getAddress();
            int portC = packet.getPort();

            String kq = XuLy(st);
            data = kq.getBytes("UTF8");
            packet = new DatagramPacket(data, data.length, ipC, portC);
            socket.send(packet);
        } catch (Exception ex) {
        }

    }

    //Xu ly khi nhan thong diep tu client
    //Tra ve chuoi se gui cho client
    private static String XuLy(String st) throws Exception {
        StringBuilder tam = new StringBuilder();
        ResultSet rs;
        DbSqlite db = new DbSqlite("mydatabase.db");
        String temp;
        switch (st.charAt(0)) {
            //Ti Gia
            case Constants.TIGIA:
                st = st.substring(2);
                String thoigian = st.substring(0, st.indexOf(','));
                String tennt = st.substring(st.indexOf(',') + 1);
                if (tennt.equalsIgnoreCase("all")) {
                    rs = db.query("select * from NGOAITE where THOIGIAN='" + thoigian + "' ORDER BY TENNT");
                } else {
                    rs = db.query("select * from NGOAITE where THOIGIAN='" + thoigian + "' AND TENNT='" + tennt + "'");
                }
                while (rs.next()) {
                    tam.append(rs.getString("TENNT"));
                    tam.append("+");
                    tam.append(rs.getString("MUATIENMAT"));
                    tam.append("+");
                    tam.append(rs.getString("MUACHUYENKHOAN"));
                    tam.append("+");
                    tam.append(rs.getString("BAN"));
                    tam.append("+");
                }
                break;
            //Gia Vang
            case Constants.GIAVANG:
                String khuvuc = st.substring(2, st.indexOf(',', 2));
                String tg = st.substring(st.indexOf(',', 2) + 1);
                if (khuvuc.equalsIgnoreCase("all")) {
                    rs = db.query("select * from GIAVANG where THOIGIAN='" + tg + "'");
                } else {
                    rs = db.query("select * from GIAVANG where KHUVUC='" + khuvuc + "' AND "
                            + "THOIGIAN='" + tg + "'");
                }
                while (rs.next()) {
                    tam.append(rs.getString("KHUVUC"));
                    tam.append("+");
                    tam.append(rs.getString("LOAI"));
                    tam.append("+");
                    tam.append(rs.getString("GIAMUA"));
                    tam.append("+");
                    tam.append(rs.getString("GIABAN"));
                    tam.append("+");
                }

                break;
            //Khu Vuc
            case Constants.KHUVUCGV:
                temp = st.substring(2);
                rs = db.query("select KHUVUC from GIAVANG where THOIGIAN='"
                        + temp + "' GROUP BY KHUVUC");
                while (rs.next()) {
                    tam.append(rs.getString("KHUVUC"));
                    tam.append("+");
                }
                rs.close();
                break;
            //Thoi Gian Cap Nhat Gia Vang
            case Constants.THOIGIANGV:
                rs = db.query("select THOIGIAN from GIAVANG GROUP BY THOIGIAN ORDER BY THOIGIAN DESC");
                while (rs.next()) {
                    tam.append(rs.getString("THOIGIAN"));
                    tam.append("+");
                }
                rs.close();
                break;
            //Thoi Gian Cap Nhat Gia Vang Co Gioi Han
            case Constants.THOIGIANGVGH:
                 temp=st.substring(2);
                 rs = db.query("select THOIGIAN from GIAVANG GROUP BY THOIGIAN ORDER BY THOIGIAN DESC LIMIT "+temp);
                while (rs.next()) {
                    tam.append(rs.getString("THOIGIAN"));
                    tam.append("+");
                }
                rs.close();
                break;

            //Thoi Gian Cap Nhat Ti Gia
            case Constants.THOIGIANTG:
                rs = db.query("select THOIGIAN from NGOAITE GROUP BY THOIGIAN ORDER BY THOIGIAN DESC");
                while (rs.next()) {
                    tam.append(rs.getString("THOIGIAN"));
                    tam.append("+");
                }
                rs.close();
                break;
            case Constants.TENTG:
                temp = st.substring(2);
                rs = db.query("select TENNT from NGOAITE where THOIGIAN='" + temp + "' GROUP BY TENNT");
                while (rs.next()) {
                    tam.append(rs.getString("TENNT"));
                    tam.append("+");
                }
                rs.close();
                break;
            default:
                return null;
        }

        db.close();
        return tam.toString();

    }
}
